package com.sitem.appsjam;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sitem.appsjam.Types.Layer;

public class LayerCheckBoxListAdapter extends BaseAdapter {

	public class LayerCheckBoxView extends LinearLayout {

	    private Layer mLayer;
	    private CheckBox mCheckBox = null;
	    
	    public LayerCheckBoxView(Context context, Layer layer) {
	         super(context);
	         mLayer = layer;
	         this.setBackgroundColor(Color.WHITE);
	         this.setOrientation(HORIZONTAL);
	         
	         if(layer.id < 0) { //Headers
		         TextView mNom = new TextView(context);
		         mNom.setText(layer.getName(context));
		         mNom.setClickable(false);
		         mNom.setFocusable(false);
		         mNom.setFocusableInTouchMode(false);
		         mNom.setTextColor(Color.WHITE);
		         mNom.setTextSize(24);
		         mNom.setGravity(Gravity.CENTER_HORIZONTAL);
		         mNom.setBackgroundColor(Color.rgb(237, 120, 4));
	        	 addView(mNom,new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
	        	 //this.setBackgroundColor(Color.rgb(237, 120, 4));
	        	 return;
	         }
	         
	         //CheckBox
	         mCheckBox = new CheckBox(context);
	         mCheckBox.setPadding(20, 0, 30, 0);
	         mCheckBox.setChecked(layer.visible);
	         mCheckBox.setOnClickListener( new OnClickListener() {
				public void onClick(View v) { mLayer.visible = (getCheckBoxState()); }
	         });         
	         addView(mCheckBox,  new LinearLayout.LayoutParams(
	                   LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

	         //Icono
	         if (layer.icon != null) {
		         ImageView mImg = new ImageView(context);
		         mImg.setImageDrawable(layer.icon);
		         mImg.setFocusable(false);
		         mImg.setClickable(false);
		         mImg.setPadding(0, 0, 5, 0);
		         mImg.setMinimumHeight(40);
		         mImg.setMinimumWidth(40);
		         mImg.setMaxHeight(40);
		         mImg.setMaxWidth(40);
		         addView(mImg, new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT));
	         }
	         
	         //Nom
	         TextView mNom = new TextView(context);
	         mNom.setText(layer.getName(context));
	         mNom.setClickable(false);
	         mNom.setFocusable(false);
	         mNom.setFocusableInTouchMode(false);
	         mNom.setTextColor(Color.BLACK);
	         mNom.setTextSize(20);
	         mNom.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
	         mNom.setGravity(Gravity.CENTER_VERTICAL);

	         //Descripcio
	         if(layer.nodes.size() > 0) { //Si sabem els nodes, aleshores directament no mostrariem layers amb numnodes = 0
		         //LinerLayout per posar el nom i desc un a sota l'altre
		         LinearLayout sublayout = new LinearLayout(context);
		         sublayout.setOrientation(VERTICAL);
	        	 sublayout.addView(mNom, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
		         
	        	 //Desc
	        	 TextView mDesc = new TextView(context);
	        	 mDesc.setText("(" + layer.nodes.size() + " punts)");
		         mDesc.setClickable(false);
		         mDesc.setFocusable(false);
		         mDesc.setFocusableInTouchMode(false);
		         sublayout.addView(mDesc, new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

		         addView(sublayout,new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
	         } else {
	        	 addView(mNom,new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
	         }
	    }

	    public void toggleCheckBoxState(){
	    	if (mCheckBox != null) {
	    		mLayer.visible = !mLayer.visible;
	    		mCheckBox.setChecked(mLayer.visible);
	    	}
	    }
	    public void setCheckBoxState(boolean bool) {
	    	mLayer.visible = bool;
	    	mCheckBox.setChecked(bool);
	    }
	    public boolean getCheckBoxState(){
	    	return mCheckBox.isChecked();
	    }
	}

	private Context mContext;
	private ArrayList<Layer> mItems = new ArrayList<Layer>();

	public LayerCheckBoxListAdapter(Context context, ArrayList<Layer> list) {
		mContext = context;
		mItems = list;
	}
	public int getCount() { 
    	return mItems.size(); 
    }
	public Layer getItem(int position) { 
    	return mItems.get(position); 
    }
    public long getItemId(int position) {
         return position;
    }
    public View getView(int position, View convertView, ViewGroup parent ){
    	return new LayerCheckBoxView(mContext, mItems.get(position));
    }
}
